<template>
  <div class="table-container">
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-form ref="form" :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item label="字典名:">
            <el-input v-model="queryForm.keyName" placeholder="请输入字典名进行查询" clearable />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="fetchData">搜索</el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>

      <vab-query-form-right-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">添加</el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table border ref="tableSort" v-loading="listLoading" :data="list" :element-loading-text="elementLoadingText" :height="height">
      <el-table-column label="字典名" show-overflow-tooltip prop="keyName"></el-table-column>
      <el-table-column label="字典值" show-overflow-tooltip prop="keyValue"></el-table-column>
      <el-table-column label="是否删除" show-overflow-tooltip align="center">
        <template #default="{ row }">
          <el-tag :type="row.isDel == 0 ? 'success' : 'warning'">
            {{ row.isDel == 0 ? '否' : '是' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" show-overflow-tooltip prop="memo"></el-table-column>
      <el-table-column label="操作" show-overflow-tooltip width="180px">
        <template #default="{ row }">
          <el-button size="mini" type="warning" @click="handleEdit(row)">编辑</el-button>
          <!-- <el-button size="mini" type="danger" @click="handleDelete(row)">删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :background="background"
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <TableEdit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { GetDictListWithKeyName, DelDict } from '@/api/table'
  import TableEdit from './components/TableEdit.vue'

  export default {
    name: 'ComprehensiveTable',
    components: {
      TableEdit,
    },
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: 'success',
          draft: 'gray',
          deleted: 'danger',
        }
        return statusMap[status]
      },
    },
    data() {
      return {
        booklist: [],
        imgShow: true,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        elementLoadingText: '正在加载...',
        queryForm: {
          keyName: '',
        },
        timeOutID: null,
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      this.fetchData()
    },
    beforeDestroy() {
      clearTimeout(this.timeOutID)
    },
    mounted() {},
    methods: {
      handleAdd() {
        this.$refs['edit'].showEdit()
      },
      handleEdit(row) {
        this.$refs['edit'].showEdit(row)
      },
      handleDelete(row) {
        this.$baseConfirm('你确定要删除当前项吗', null, async () => {
          const { msg } = await DelDict({ dictId: row.id })
          this.$baseMessage(msg, 'success')
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      handleQuery() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await GetDictListWithKeyName(this.queryForm)
        this.list = data
        console.log(data)

        this.timeOutID = setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>

<style scoped lang="scss">
  .booksinfo {
    display: flex;
    .bookname {
      margin-left: 10px;
      font-size: 700;
    }
    .bookauthor {
      margin-left: 10px;
    }
  }
</style>
